var ip = 'http://' + document.location.host;

//弹窗返回按钮
$('.closeBtn').on('click',function() {
	location.reload();
	window.parent.document.getElementById("alert_shade").style.display="none";
	window.parent.document.getElementById("add_user_box").style.display="none";
});

//初始化订阅checkbox
$.ajax({
	url : "/subscribe/findAllSubscribe",
	type : "GET",
	dataType : "json",
	success : function(res) {
		if (res.code == 200) {
			for (var i = 0; i < res.data.length; i++) {
				$("#subscribeDiv").append("<input type='checkbox' value='"+res.data[i].functionName+"'name='subscribeCheckbox' id='subscribeCheckbox'/>"+res.data[i].functionName);
			}
		}
	}
})

//法院ztree
$(function() {
    initTree();
})

	var setting_ztree = {
	    view: {
	        showLine: true,
	        showIcon:true,
	        selectedMulti : true,
	        dblClickExpand: false,
	        addHoverDom: addHoverDom,
	        removeHoverDom: removeHoverDom
	    },check : {
	        enable : true,
	        chkStyle : "radio",
	        radioType : "all"
	    },
	    data: {
	        simpleData: {
	            enable: true
	        }
	    },
	    callback: {
	        //onClick: onClick_ztree,
	        /*onCheck: onClick_ztree*/
	    }
	};
	function addHoverDom(treeId, treeNode) {
	    $("#" + treeNode.tId).addClass("hover");
	};
	function removeHoverDom(treeId, treeNode) {
	    $("#" + treeNode.tId).removeClass("hover");
	};
	function filter(node) {
		var fybm = node.id;
		var fymc = node.name;
		//if((fybm.length==3&&fybm.substr(2,1)!='0'&&(fybm.substr(0,1)=='1'||fybm.substr(0,1)=='2'||fybm.substr(0,1)=='M'||fybm.substr(0,1)=='9'))||fymc.indexOf("上海金融法院")!=-1||fymc.indexOf("矿区")!=-1||fymc.indexOf("知识产权")!=-1||fymc.indexOf("铁路运输")!=-1||fymc.indexOf("人民解放军")!=-1||fymc.indexOf("海事法院")!=-1||(fybm.length==3&&fybm=='000')||(fybm.length==3&&fybm.substring(0,2)=='79')||(fybm.length==3&&fybm.substring(0,2)=='7C')||(fybm.length==3&&fybm.substring(0,2)=='8J')||(fybm.length==3&&fybm.substring(0,2)=='SG')||(fybm.length==3&&fybm.substring(0,2)=='8G')||(fybm.length==3&&fybm.substring(0,2)=='6G')||(fybm.length==3&&fybm.substring(0,2)=='L3')||(fybm.length==3&&fybm.substring(0,2)=='L5')||(fybm.length==3&&fybm.substring(0,2)=='L6')||(fybm.length==3&&fybm.substring(0,2)=='16')||(fybm.length==3&&fybm.substring(0,2)=='93')||(fybm.length==3&&fybm.substring(0,2)=='HD')){//||(fybm.length==3&&fybm.substring(2,3)!='0')||(fybm.length==3&&fybm.substring(2,1)!='0')	
		if(fymc.indexOf("全部")!=-1||fybm.length==4||fymc.indexOf("上海金融法院")!=-1||fymc.indexOf("矿区")!=-1||fymc.indexOf("知识产权")!=-1||fymc.indexOf("铁路运输")!=-1||fymc.indexOf("人民解放军")!=-1||fymc.indexOf("海事法院")!=-1||(fybm.length==3&&fybm.substring(0,2)=='79')||(fybm.length==3&&fybm.substring(0,2)=='7C')||(fybm.length==3&&fybm.substring(0,2)=='8J')||(fybm.length==3&&fybm.substring(0,2)=='SG')||(fybm.length==3&&fybm.substring(0,2)=='8G')||(fybm.length==3&&fybm.substring(0,2)=='6G')||(fybm.length==3&&fybm.substring(0,2)=='L3')||(fybm.length==3&&fybm.substring(0,2)=='L5')||(fybm.length==3&&fybm.substring(0,2)=='L6')||(fybm.length==3&&fybm.substring(0,2)=='16')||(fybm.length==3&&fybm.substring(0,2)=='93')||(fybm.length==3&&fybm.substring(0,2)=='HD')){
			return node;
		}
	} 
	var initTree = function(){
	        $.ajaxSettings.async = false;
	        $.post( "/user/tree", {}, function(response) {
	            var zNodes = response.data;
	            zTreeObj = $.fn.zTree.init($("#partTree"), setting_ztree, zNodes);
	            var nodes = zTreeObj.getNodes();
	            if (nodes.length>0) {
	                for(var i=0;i<nodes.length;i++){
	                    zTreeObj.expandNode(nodes[i], true, false, false);//默认展开第一级节点
	                }
	            }
	            var nodescheck = zTreeObj.getNodesByFilter(filter);
	            for(var i=0;i<nodescheck.length;i++){
	            	nodescheck[i].nocheck = true;
	                zTreeObj.updateNode(nodescheck[i]);
	            }
	        });
	        $.ajaxSettings.async = true;
	}

	
	//用户输入判断
	$('.saveBtn').on('click', function() {
		var userip = $('#userip').val();
		var username = $('#username').val();
		var password = $('#passwords').val();
		var zTree = $.fn.zTree.getZTreeObj("partTree");
		var nodes = zTree.getCheckedNodes();
		var fybm;
		var fymc;
		if(!userip){
			layer.tips("ip不能为空！", '#userip', {time: 2000,tips: [3,"red"]});
			return false;
		}
		if(!username){
			layer.tips("姓名不能为空！", '#username', {time: 2000,tips: [3,"red"]});
			return false;
		}
		if(!password){
			layer.tips("密码不能为空！", '#passwords', {time: 2000,tips: [3,"red"]});
			return false;
		}
		if(password.length<6 && password.length>0){
			layer.tips("请输入至少6位的密码！", '#passwords', {time: 2000,tips: [3,"red"]});
			$('#passwords').val("");
			return false;
		}
		if(nodes.length==0){
			layer.tips("所在法院不能为空！", '#partTree', {time: 2000,tips: [3,"red"]});
			return false;
		}else{
			fybm = nodes[0].id;
			fymc = nodes[0].name;
		}
		
//		新增用户信息保存
		$.ajax({
			type: 'POST',
	        url: ip+'/user/save',
	        data:{"userip":userip,"name":username,"password":password,"fybm":fybm,"fymc":fymc},
	        success: function(res) {
	        	if(res.code == 200){
	        		location.reload();
	        		parent.location.reload();
	        	}else if(res.code == 401){
	        		layer.alert(res.msg, {icon: 5,title: "提示"});
	        	}
	            
	        }
		})
	});